//
//  HHDiscoverViewController.m
//  gitosc01
//
//  Created by hjl on 16/3/20.
//  Copyright © 2016年 hjl. All rights reserved.
//

#import "HHDiscoverViewController.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import "HHLanguage.h"
#import "HHLanguageProjectController.h"
#import "HHProject.h"
#import "HHInfoViewController.h"

@interface HHDiscoverViewController () <UITableViewDelegate>

/** languages */
@property (nonatomic, strong) NSMutableArray *languages;
/** 项目模型数组 */
@property (strong, nonatomic) NSMutableArray *projects;
@end

@implementation HHDiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;

    NSString *strUrl = @"https://git.oschina.net/api/v3//projects/languages";
    // 网络请求
    [[AFHTTPSessionManager manager] GET:strUrl parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        //  字典转模型
        self.languages = [HHLanguage mj_objectArrayWithKeyValuesArray:responseObject];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        nil;
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.languages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 创建cell
    static NSString *languageCell = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:languageCell];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:languageCell];
    }
    // 设置cell配件类型
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // 获取模型数据
    HHLanguage *model = self.languages[indexPath.row];
    
    // 把模型数据设置给cell
    cell.textLabel.text = model.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 控制器
    HHLanguageProjectController *languageVc = [[HHLanguageProjectController alloc]init];
    // 模型
    HHLanguage *language = [self.languages objectAtIndex:indexPath.row];
    
    languageVc.title = language.name;
    languageVc.languageID = language.ID;

    [languageVc setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:languageVc animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

@end
